/*
 * -------------------------------------------------------------------------------------------------------*
 * SBS Priceboard V2.0
 * Copyright (C) 2008 Unicom Vietnam JSC
 * -------------------------------------------------------------------------------------------------------* 
 * HeadURL: http://192.168.1.4/svn/priceboard/trunk/SBS_PriceboardV2/front-end/end-user/enduser_priceboard/
 * LastChangeBy:
 * LastChangeDate:
 */
package com.unicom.priceboard.data.hose;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.apache.log4j.Logger;

import com.unicom.priceboard.data.vo.FRoomElement;
import com.unicom.priceboard.thread.FroomThread;
import com.unicom.priceboard.util.Constants;
import com.unicom.priceboard.util.Util;

/**
 * Creation date: Jul 16, 2008
 * @author quan
 */
public class FRoomReader {
	private static final Logger logger = Logger.getLogger(FRoomReader.class);
	
	public void parseFroom(String path,int newold){
		try{
			 FileInputStream readFRoom =new FileInputStream(Util.getCurrentDataPath(path));
			 generateFRoomList(readFRoom,newold);
			 readFRoom.close();
		}catch(Exception e){
			logger.debug("not read file: "+e.getMessage());
		}
	}
	
	private synchronized void generateFRoomList(InputStream is,int newold)
	{
		DataInputStream input = new DataInputStream(is);
		try{
			while(input.available()>0)
			{
				FRoomElement record = new FRoomElement();
				
				//Create a new ByteBuffer to store a record from file
				ByteBuffer buf = ByteBuffer.allocate(20);
				// Set to little endian
				buf.order(ByteOrder.LITTLE_ENDIAN);
				
				// Get each field from the file and put it to the byte buffer
				buf.putInt(input.readInt());//StockNo
				buf.putDouble(input.readDouble());//TotalRoom
				buf.putDouble(input.readDouble());//CurrentRoom
				
				//Reset position for reading
				buf.flip();
				// Set to big endian
				buf.order(ByteOrder.BIG_ENDIAN);

				// Retrieve data from byte buffer
				record.setStockNo(buf.getInt());
				record.setTotalRoom(buf.getDouble());
				record.setCurrentRoom(buf.getDouble());
				//save hashmap with key is stockNo
				if(newold==Constants.FROOMNEW){
					FroomThread.lstFRoom.put(record.getStockNo(), record);
				}else{
					FroomThread.oldFRoom.put(record.getStockNo(), record);
				}
			}			
		}catch(Exception ex)
		{
			//logger.info("End of file: " + ex);
		}
	}
	
	
//	private synchronized void generateOldFRoom(InputStream is) {
//		DataInputStream input = new DataInputStream(is);
//		try{
//			while(input.available()>0)
//			{
//				FRoomElement record = new FRoomElement();
//				
//				//Create a new ByteBuffer to store a record from file
//				ByteBuffer buf = ByteBuffer.allocate(20);
//				// Set to little endian
//				buf.order(ByteOrder.LITTLE_ENDIAN);
//				
//				// Get each field from the file and put it to the byte buffer
//				buf.putInt(input.readInt());//StockNo
//				buf.putDouble(input.readDouble());//TotalRoom
//				buf.putDouble(input.readDouble());//CurrentRoom
//				
//				//Reset position for reading
//				buf.flip();
//				// Set to big endian
//				buf.order(ByteOrder.BIG_ENDIAN);
//
//				// Retrieve data from byte buffer
//				record.setStockNo(buf.getInt());
//				record.setTotalRoom(buf.getDouble());
//				record.setCurrentRoom(buf.getDouble());
//				//save hashmap with key is stockNo
//				FroomThread.oldFRoom.put(record.getStockNo(), record);
//				
//			}			
//		}catch(Exception ex)
//		{
//			//logger.info("End of file: " + ex);
//		}
//	}
}
